package main.chapter4

import java.io.File

fun main(args: Array<String>) {
    val map = HashMap<Char, Int>()
    //读取文件，转换为字符数组，过滤掉空字符
    File("src/main/chapter4/CountFile.txt")
        .readText().toCharArray()
        .filterNot(Char::isWhitespace)
        .forEach {
            val count = map[it]
            if (count == null) map[it] = 1
            else map[it] = count + 1
        }

    map.forEach(::println)

    File("src/main/chapter4/CountFile.txt")
        .readText().toCharArray()
        .filterNot(Char::isWhitespace)
        .groupBy { it }.map {
            it.key to it.value.count()
        }.forEach(::println)
}